<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\controller\api\store\user;

use app\jobs\BatchHandleJob;
use app\Request;
use app\services\activity\coupon\StoreCouponIssueServices;
use app\services\activity\coupon\StoreCouponUserServices;
use app\services\other\queue\QueueServices;
use app\services\store\StoreUserServices;
use app\services\store\SystemStoreStaffServices;
use app\services\user\group\UserGroupServices;
use app\services\user\label\UserLabelCateServices;
use app\services\user\label\UserLabelRelationServices;
use app\services\user\level\SystemUserLevelServices;
use app\services\user\level\UserLevelServices;
use app\services\user\UserBatchProcessServices;
use app\services\user\UserServices;
use think\Response;

/**
 *
 * Class User
 * @package app\controller\api\store\user
 */
class User
{
	protected $services;


	/**
	 * @var int
	 */
	protected $uid;
	/**
	 * 门店店员信息
	 * @var array
	 */
	protected $staffInfo;
	/**
	 * 门店id
	 * @var int|mixed
	 */
	protected $store_id;

	/**
	 * 门店店员ID
	 * @var int|mixed
	 */
	protected $staff_id;

	/**
	 * 构造方法
	 * User constructor.
	 * @param UserServices $services
	 */
	public function __construct(UserServices $services, Request $request)
	{
		$this->services = $services;
		$this->uid = (int)$request->uid();
	}

	/**
	 * @return void
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	protected function getStaffInfo()
	{
		/** @var SystemStoreStaffServices $staffServices */
		$staffServices = app()->make(SystemStoreStaffServices::class);
		$this->staffInfo = $staffServices->getStaffInfoByUid($this->uid)->toArray();
		$this->store_id = (int)$this->staffInfo['store_id'] ?? 0;
		$this->staff_id = (int)$this->staffInfo['id'] ?? 0;
	}


    public function list(Request $request): Response
    {
        $where = $request->getMore([
            ['page', 1],
            ['limit', 20],
            ['nickname', ''],
            ['status', ''],
            ['pay_count', ''],
            ['is_promoter', ''],
            ['order', ''],
            ['data', ''],
            ['user_type', ''],
            ['country', ''],
            ['province', ''],
            ['city', ''],
            ['user_time_type', ''],
            ['user_time', ''],
            ['sex', ''],
            [['level', 0], 0],
            [['group_id', 'd'], 0],
            [['label_id', 'd'], 0],
            ['now_money', 'normal'],
            ['field_key', ''],
            ['isMember', ''],
            ['label_ids', '']
        ]);
		$this->getStaffInfo();
        if ($where['label_ids']) {
            $where['label_id'] = stringToIntArray($where['label_ids']);
            unset($where['label_ids']);
        }
        $where['user_time_type'] = $where['user_time_type'] == 'all' ? '' : $where['user_time_type'];

		if ($where['nickname']) {
			$where['is_filter_del'] = 1;
			$data = $this->services->index($where);
		} else {
			$storeUserServices = app()->make(StoreUserServices::class);
			$data = $storeUserServices->index($where, $this->store_id);
		}
        return app('json')->success($data);
    }

	/**
	 * 用户详情
	 * @param $id
	 * @return Response
	 */
    public function info($uid)
    {
        if (!$uid) {
            return app('json')->fail('用户id不能为空');
        }
        return app('json')->success($this->services->manageRead((int)$uid));
    }

	/**
	 * 获取用户标签
	 * @param $uid
	 * @param UserLabelCateServices $services
	 * @return Response
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
    public function userLabel($uid, UserLabelCateServices $services)
    {
        return app('json')->success($services->getUserLabel((int)$uid));
    }

	/**
	 * 分组
	 * @param UserGroupServices $services
	 * @return Response
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
    public function userGroup(UserGroupServices $services)
    {
        return app('json')->success($services->getGroupList('*'));
    }

	/**
	 * 等级
	 * @param SystemUserLevelServices $services
	 * @return Response
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
    public function userLevel(SystemUserLevelServices $services)
    {
        return app('json')->success($services->getLevelList(['is_show' => 1, 'is_del' => 0], 'id,name,grade,image,icon'));
    }

	/**
	 * 优惠券列表
	 * @param Request $request
	 * @param StoreCouponIssueServices $services
	 * @param StoreCouponUserServices $storeCouponUserServices
	 * @return Response
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
    public function couponGrant(Request $request, StoreCouponIssueServices $services, StoreCouponUserServices $storeCouponUserServices)
    {
        [$coupon_title, $uid] = $request->getMore([
            ['coupon_title', ''],
            ['uid', 0],
        ],true);
        if ($uid == 0) {
            $where['receive'] = 'send';
            $where['coupon_title'] = $coupon_title;
            $data = $services->getCouponIssueList($where);
        } else {
            $data = $storeCouponUserServices->getUserCouponList($uid, 0);
        }
        return app('json')->success($data);
    }


}
